/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.chooser.view;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.text.Element;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import offset.nodes.client.chooser.model.FolderNode;
import offset.nodes.client.chooser.model.TypeIconProvider;

public class SimpleNodeRenderer extends DefaultTreeCellRenderer {
    static ImageIcon defaultIcon = new ImageIcon(offset.nodes.client.chooser.view.SimpleNodeRenderer.class.getResource("/offset/nodes/client/view/resources/defaulttype.png"));

    public Component getTreeCellRendererComponent(JTree tree, Object value,
			  boolean sel,
			  boolean expanded,
			  boolean leaf, int row,
			  boolean hasFocus) {
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
        ImageIcon nodeIcon = defaultIcon;
        if (treeNode.getUserObject() != null
                && treeNode.getUserObject() instanceof FolderNode) {
            ImageIcon nodeTypeIcon = getNodeTypeIcon((FolderNode) treeNode.getUserObject());
            if (nodeTypeIcon != null)
                nodeIcon = nodeTypeIcon;
        }

        setEnabled(tree.isEnabled());
        setText(value.toString());
        if (selected)
            setBackground(getBackgroundSelectionColor());
        else
            setBackground(getBackgroundNonSelectionColor());
        
        setIcon( nodeIcon );
        
        return this;
    }
    
    /**
     * Return the image icon for the node type
     *
     * @param node the folder node to return the type for
     * @return the image icon
     */
    protected ImageIcon getNodeTypeIcon(FolderNode node) {
        return TypeIconProvider.getInstance().getNodeTypeIcon(node.getType());
    }    

}
